import type { Directive, DirectiveBinding } from 'vue'
import { hasAuth } from '@/router/utils'

/**
 * 权限指令
 */
export const auth: Directive = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding
    if (value[0] == undefined) return
    if (value) {
      if (el instanceof HTMLButtonElement || el instanceof HTMLInputElement) {
        // 启用或禁用按钮
        el.disabled = !hasAuth(value)
      }
    } else {
      throw new Error("[Directive: auth]: need auths! Like v-auth=\"['btn.add','btn.edit']\"")
    }
  }
}
